package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.Result;
import com.itheima.pojo.AddAddress;
import com.itheima.pojo.Address;
import com.itheima.service.AddressService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/address")
public class AddressController {
    @Reference
    AddressService addressService;

    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Address> addressList=addressService.findAll();
            return new Result(true, "查询地图所有数据成功",addressList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询地图所有数据失败");
        }
    }

    @RequestMapping("/selectAddress")
    public Result selectAddress() {
        try {
            List<AddAddress> selectAddress= new ArrayList<>();
            List<Address> addressList=addressService.findAll();
            if(addressList!=null&&addressList.size()>0){
                for (Address address : addressList) {
                    AddAddress addAddress = new AddAddress(address.getAddressName(), address.getId());
                    selectAddress.add(addAddress);
                }
            }
            return new Result(true, "查询地图所有数据成功",selectAddress);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询地图所有数据失败");
        }
    }

    @RequestMapping("/findAddressName")
    public String findAddressName(int id) {
        return addressService.findAddressNameById(id);
    }
}